package tools

import (
	"fmt"
	"gitee.com/jjawesomejj/awesome-util/event"
	"io"
)

type Standard struct {
	Stdout     *StdWriterWithPid
	Stderr     *StdWriterWithPid
	StdIn      io.Reader
	TaskId     string
	ServerName string
}

func (Standard *Standard) GetOutput() string {
	return "服务器名称:" + Standard.ServerName + "\n" + "配置ID:" + Standard.TaskId + "\n" + "输出:" + Standard.Stdout.GetDataString()
}

func GetTaskStand(TaskId string, ServerName string, standardPool *[]*Standard) *Standard {
	std := StdWriterWithPid{}
	onWriteFun := func(bytes []byte) {
		data := map[string]interface{}{
			"task_id":     TaskId,
			"server_name": ServerName,
			"content":     string(bytes),
			"event":       "onlog",
		}
		fmt.Println(ServerName + ":" + string(bytes))
		event.Dispatch("onlog", data)
	}
	std.OnWrite = onWriteFun
	item := Standard{
		Stdout:     &std,
		Stderr:     &std,
		TaskId:     TaskId,
		ServerName: ServerName,
	}
	*standardPool = append(*standardPool, &item)
	return &item
}
